package com.jianan.mis.app.other;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.jianan.mis.sys.IImageKeys;
import com.jianan.mis.sys.ImageManager;
import com.jianan.mis.sys.SpringBeanFactory;

public class PloneView extends ViewPart {

	public static final String ID = "com.jianan.mis.app.other.PloneView"; //$NON-NLS-1$

	private Browser browser;
	
	private String url = ((Entries) SpringBeanFactory.getBean("entries")).getEntry("plone");
	
	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout());

		browser = new Browser(container, SWT.NONE);
		final GridData gd_browser = new GridData(SWT.FILL, SWT.FILL, true, true);
		browser.setLayoutData(gd_browser);
		browser.setUrl(url);
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}
	
	private ForwardAction forwardAction = new ForwardAction();
	private BackAction backAction = new BackAction();
	private RefreshAction refreshAction = new RefreshAction();
	private HomeAction homeAction = new HomeAction();
	
	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
		toolbarManager.add(forwardAction);
		toolbarManager.add(backAction);
		toolbarManager.add(refreshAction);
		toolbarManager.add(homeAction);
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
		menuManager.add(forwardAction);
		menuManager.add(backAction);
		menuManager.add(refreshAction);
		menuManager.add(homeAction);
	}
	
	private class ForwardAction extends Action {
		
		public final static String ID = "com.jianan.app.com.jianan.mis.app.other.ploneForward";

		public ForwardAction() {
			setId(ID);
			setActionDefinitionId(ID);
			setText("前进(&F)");
			setToolTipText("前进");
			setImageDescriptor(ImageManager.getImageDescriptor(IImageKeys.RIGHT));
		}
		
		public void run() {
			if (browser.isForwardEnabled()) {
				browser.forward();
			}
		}
	}
	
	private class BackAction extends Action {
		
		public final static String ID = "com.jianan.app.com.jianan.mis.app.other.ploneBack";

		public BackAction() {
			setId(ID);
			setActionDefinitionId(ID);
			setText("后退(&B)");
			setToolTipText("后退");
			setImageDescriptor(ImageManager.getImageDescriptor(IImageKeys.LEFT));
		}
		
		public void run() {
			if (browser.isBackEnabled()) {
				browser.back();
			}
		}
	}
	
	private class RefreshAction extends Action {
		
		public final static String ID = "com.jianan.app.com.jianan.mis.app.other.ploneRefresh";

		public RefreshAction() {
			setId(ID);
			setActionDefinitionId(ID);
			setText("刷新(&R)");
			setToolTipText("刷新");
			setImageDescriptor(ImageManager.getImageDescriptor(IImageKeys.REFRESH));
		}
		
		public void run() {
			browser.refresh();
		}
	}
	
	private class HomeAction extends Action {
		
		public final static String ID = "com.jianan.app.com.jianan.mis.app.other.ploneHome";

		public HomeAction() {
			setId(ID);
			setActionDefinitionId(ID);
			setText("CMS首页(&H)");
			setToolTipText("CMS首页");
			setImageDescriptor(ImageManager.getImageDescriptor(IImageKeys.FAMILY_CONDITION));
		}
		
		public void run() {
			browser.setUrl(url);
		}
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

}
